Package aspect.example

Source Code of aspect.example.PlayerShip

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aspect.example;

import static aspect.core.AspectRenderer.*;
import aspect.entity.Entity;
import aspect.entity.behavior.Behavior;
import aspect.physics.Motion;
import aspect.physics.RigidBody;
import aspect.physics.Time;
import aspect.util.Matrix4x4;
import aspect.util.Trig;
import aspect.util.Vector3;
import static org.lwjgl.input.Keyboard.*;
import org.lwjgl.input.Mouse;
import static org.lwjgl.input.Mouse.getX;
import static org.lwjgl.input.Mouse.getY;
import static org.lwjgl.input.Mouse.setCursorPosition;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.glu.GLU;

/**
*
* @author vincent
*/
public class PlayerShip extends Entity {

    public float mouseSensitivity = 0.01f;

    public PlayerShip() {
        Mouse.setGrabbed(true);
        addBehavior(new ShipControl());
        addBehavior(new RigidBody());
        centerMouse();
    }

    private class ShipControl extends Behavior {

        @Override
        public void update() {
            float yaw = -getMouseDX() * mouseSensitivity;
            float pitch = getMouseDY() * mouseSensitivity;
           
            yaw = Math.min(Math.abs(yaw), 120) * Math.signum(yaw);
            pitch = Math.min(Math.abs(pitch), 120) * Math.signum(pitch);

            transform.rotateLeft(yaw * Time.deltaTime());
            transform.rotateUp(pitch * Time.deltaTime());
           
            if (isKeyDown(KEY_A)) {
                transform.rollRight(-Trig.QUARTER_CIRCLE * Time.deltaTime());
            }
           
            if (isKeyDown(KEY_D)) {
                transform.rollRight(Trig.QUARTER_CIRCLE * Time.deltaTime());
            }
           
            float thrust = 0.0f;
           
            if (isKeyDown(KEY_W)) {
                thrust = 4.0f;
            } else if (isKeyDown(KEY_S)) {
                thrust = -4.0f;
            }
           
            RigidBody rb = ent.rigidBody();
            rb.impel(transform.forward().times(thrust * Time.deltaTime()));

            if (rb.velocity.mag() > 2.0f) {
                rb.velocity = rb.velocity.normalize().times(2.0f);
            }
        }
    }

    private int getMouseDX() {
        return getX() - getCenterX();
    }

    private int getMouseDY() {
        return getY() - getCenterY();
    }

    private int getCenterX() {
        return Display.getWidth() / 2;
    }

    private int getCenterY() {
        return Display.getHeight() / 2;
    }
   
    private void centerMouse() {
        setCursorPosition(getCenterX(), getCenterY());
    }

}
TOP

Related Classes of aspect.example.PlayerShip

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.